<?php
error_reporting(1);

require_once(APPPATH . '/controllers/test/Toast.php');

class Property_tests extends Toast {
	 
	var $CI;

	function Property_tests() {
		
		parent::Toast(__FILE__);
		$this->load->model('Property_model','',TRUE);
		// Load any models, libraries etc. you need here
	}

	function _pre() {

		 
	}
	function _post(){
		 
	}
	// Add new property adv test
	function test_save() {
	
		$result = "";
		$propertyadv['title'] = '2 Room aparment to sell in Choa Chu Kang';
		$propertyadv['description'] = "4 minutes away from MRT. Good vicinity";
		$propertyadv['price'] = 120000000;
		$propertyadv['categoryID'] = 2; //Sell
		$propertyadv['advertiserID'] = 1;
		$propertyadv['typeID'] = 1;
		
		$features['bedroom'] = 2;
		$features['bathroom'] = 1;
		$features['Livingroom'] = 2;
		$features['squareft'] = "1290sqft";
		$features['condition'] = 'good';
		
		$propertyadv['features'] = serialize($features);
		
		$propertyadv['address'] = "Blk 338D #19-10 Tah Ching Road";
		$propertyadv['contact_person'] = 'Mr.Simon';
		$propertyadv['contact_number'] = '6789 9900';
		
		
		$id = $this->Property_model->save($propertyadv);
		
		$result = $result . "Add new adv in ID : " . $id;
		$this->message = $result;
		$this->_assert_equals($id, 2);
	}
	
	// Update advertisement test
	function test_update(){
		$cond = array();
		$cond['propertyID'] = 1;
		
		$result = $this->Property_model->get_result($cond);
		$newadv = array();
		$newadv = $result[0];
		$newadv['title'] = 'Jumbo House for sales with Cheap Price';
		//$newadv['advid'] = $adv['advid'];
		$this->Property_model->update($newadv);
		
		$updatedresult = $this->Property_model->get_result($cond);
		
		foreach ($updatedresult as $updatedadvertisement){
			$result = "";
			$this->message = $updatedadvertisement['title'] .  ' adversier:' . $updatedadvertisement['firstName'];
			$this->_assert_equals($updatedadvertisement['title'],'Jumbo House for sales with Cheap Price');
		}	
	}
	// delete advertisement test
	
	function test_delete(){
		$total_records = $this->advertisementModel->count_all();
		
		$this->Property_model->delete(3);
		$result = "";
		$this->message = "No of records left in total " . $this->advertisementModel->count_all();
		$this->_assert_equals($total_records - 1, $this->advertisementModel->count_all());
		
	}
	
	//get by advertiser id
	function test_result(){
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'desc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		
		$result_list = $this->Property_model->get_result(NULL,10,0,$sort);
		$message = "";
		foreach($result_list as $result){
			$message = $message . '<br>'. $result['title']. '<br>';					
		}
		$this->message = $message;
		$this->_assert_equals(count($result_list), 3);
	}
	
	//test price
	function test_price(){
		$price_range = array('low' => 0, 'high' => 99999999.99);
		$cond = array('categoryID' => 2);
		$result_list = $this->Property_model->get_by_pricerange($price_range, $cond);
		$message = "";
		foreach($result_list as $result){
			$message = $message . '<br>'. $result['title']. '<br>';
		}
		$this->message = $message;
		$this->_assert_equals(count($result_list), 2);
	}
	//test search
	function test_search(){
		$search = 'apartment';
		$result_list = $this->Property_model->get_search_result($search);
		$message = "";
		foreach($result_list as $result){
			$message = $message . '<br>'. $result['title']. '<br>';
		}
		$this->message = $message;
		$this->_assert_equals(count($result_list), 2);
	}

}
?>
